﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Moon.Common;
using Moon.Common.WebInfo;
using System.IO;

public partial class Admin_NewManage_NewList : System.Web.UI.UserControl
{
    GPI.Server.NewListService server = new GPI.Server.NewListService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            object obj = Request.QueryString["ID"];
            if (obj != null)
            {
                this.ddlNodeList.DataSource = new GPI.Server.NodeService().GetList("ModelType='NewList'").Tables[0];
                this.ddlNodeList.DataBind();
                this.hfNodeID.Value = obj.ObjectToInt(0).ToString();
                Bind();
            }
        }
    }
    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Model.NewListInfo model = new Model.NewListInfo();
        model.ID = this.btnSave.CommandArgument.ToInt(0);
        model.keywords=this.txtkeywords.Text;
        model.description=this.txtdescription.Text;
        model.NewContent=this.txtNewContent.Text;
        model.NewTitle=this.txtNewTitle.Text;
        model.NewInfo=this.txtNewInfo.Text;
        model.NodeID = this.ddlNodeList.SelectedValue.ToInt(0);
        model.TemplateID = this.ddlTemplateList.SelectedValue.ToInt(0);
        if (this.hfZoomImagePath.Value.IsNullOrEmpty())
            model.ZoomImage = !this.fuZoomImagePath.FileName.IsNullOrEmpty() ? WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuZoomImagePath.FileName.GetFileExt() : "";
        else if (("images/no_image.jpg").Equals(this.hfZoomImagePath.Value.ToString()))
            model.ZoomImage = !this.fuZoomImagePath.FileName.IsNullOrEmpty() ? WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuZoomImagePath.FileName.GetFileExt() : "";
        else
            model.ZoomImage = this.hfZoomImagePath.Value;
        upFile(this.fuZoomImagePath, model.ZoomImage, this.Application["WebPath"].ToString(), true, WebConfigInfo.Imgformat);
        bool isTrue = false;
        if (model.ID == 0)
        {
            isTrue = server.Add(model);
        }
        else
        {
            isTrue = server.Update(model);
        }

        if (isTrue)
        {
            Bind();
            MessageBox.Show(this.Page,"操作成功！");
        }else
        {
            Bind();
            MessageBox.Show(this.Page, "操作失敗！");
        }
    }

        /// <summary>
    /// 列表方法
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repNewList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = e.CommandArgument.ObjectToInt(0);
        switch (e.CommandName)
        {
            case "edit":
                this.MultiView1.ActiveViewIndex = 1;
                Model.NewListInfo model = server.GetModel(id);
                this.txtdescription.Text = model.description;
                this.txtkeywords.Text = model.keywords;
                this.txtNewContent.Text = model.NewContent;
                this.txtNewInfo.Text = model.NewInfo;
                this.txtNewTitle.Text = model.NewTitle;
                this.btnSave.CommandArgument = model.ID.ToString();
                this.hfZoomImagePath.Value = model.ZoomImage;
                if (ddlNodeList.Items.FindByValue(model.NodeID.ToString()) != null)
                this.ddlNodeList.SelectedValue = model.NodeID.ToString();
                this.ddlTemplateList.DataSource = new GPI.Server.TemplateService().GetList("TempletaType=1");
                this.ddlTemplateList.DataBind();
                if (this.ddlTemplateList.Items.FindByValue(model.TemplateID.Value.ToString()) != null)
                {
                    this.ddlTemplateList.SelectedValue = model.TemplateID.Value.ToString();
                }
                break;
            case "delete":
                if (server.Delete(id))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                Bind();
                break;
        }
    }

    /// <summary>
    /// 返回列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReturnList_Click(object sender, EventArgs e)
    {
        Bind();
    }


    #region 列表方法
    private void Bind()
    {
        //获得总数
        int count = server.GetRecordCount("NodeID="+this.hfNodeID.Value);


        //切换至列表页
        this.MultiView1.ActiveViewIndex = 0;
        //设置每页信息数
        this.anpNewList.PageSize = 10;
        //信息总数
        this.anpNewList.RecordCount = count;
        //当前索引数
        int _CurrentPageIndex = this.anpNewList.CurrentPageIndex;
        //页面信息数
        int _PageSize = this.anpNewList.PageSize;
        //开始标识
        int starIndex = (_CurrentPageIndex - 1) * _PageSize <= 0 ? 0 : ((_CurrentPageIndex - 1) * _PageSize) + 1;
        // 结束标识
        int endIndex = _CurrentPageIndex * _PageSize;
        //根据标识搜索绑定数据表
        string[] orderstr = { "ID", "DESC" };
        this.repNewList.DataSource = server.GetListByPage(" and NodeID=" + this.hfNodeID.Value, orderstr, starIndex, endIndex);
        this.repNewList.DataBind();
        this.txtdescription.Text = "";
        this.txtkeywords.Text = "";
        this.txtNewContent.Text = "";
        this.txtNewInfo.Text = "";
        this.txtNewTitle.Text="";
       
    }
    #endregion
    /// <summary>
    /// 切换到信息界面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnCreateNew_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
        if(ddlNodeList.Items.FindByValue( this.hfNodeID.Value)!=null)
        this.ddlNodeList.SelectedValue = this.hfNodeID.Value;
        this.ddlTemplateList.DataSource = new GPI.Server.TemplateService().GetList("TempletaType=1");
        this.ddlTemplateList.DataBind();
    }
    /// <summary>
    /// 分页方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void anpNewList_PageChanged(object sender, EventArgs e)
    {
        Bind();

    }


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool upFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, bool isNULL, string CheckType)
    {
        //判断上传文件是否是空
        if (!Fupload.HasFile && isNULL)
        {
            return true;
        }
        //判断目录是否存在不存在则创建
        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
        }
        //删除久的文件
        if (new FileInfo(dir + SaveName).Exists)
        {
            CoFileHepler.DeleteFile(dir + SaveName);
        }
        try
        {
            //獲得文件類型
            string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
            //判斷文件類型是否為需要的類型
            if (CoFileHepler.CheckValidExt(type, CheckType))
            {
                Fupload.PostedFile.SaveAs(dir + SaveName);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
    protected void lbnAddContentInfo_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
    }
}
